// pages/order/order.js
const ins = getApp()

Page({
  data: {
    customBar: ins.globalData.CustomBar,
    currtab: 0,
    swipertab: [
      { name: '我发起的', index: 0 },
      { name: '我收到的', index: 1 }
    ],
    role_type: {
      'SEEKER': '求兼职',
      'EMPLOYER': '招兼职'
    },
    status_text: [
      '待处理',
      '进行中',
      '已结束',
      '已拒绝',
      '已取消'
    ],
    request: {
      issue: {
        list: [],
        page: {
          page: 1,
          offset: 10
        },
        onShow: false,
        hasNextPage: true,
        refresher: false,
      },
      receive: {
        list: [],
        page: {
          page: 1,
          offset: 10
        },
        onShow: false,
        hasNextPage: true,
        refresher: false,
      },
      fetching: false
    }
  },
  onLoad: function () {
    
  },
  onShow: function () {
    this.setData({
      'request.issue.page.page': 1,
      'request.issue.hasNextPage': true,
      'request.issue.onShow': true,
      'request.receive.page.page': 1,
      'request.receive.hasNextPage': true,
      'request.receive.onShow': true,
    }, () => {
      this.fetchRequest()
    })
  },
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },
  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
    this.orderShow()
  },
  reload(e) {
    const is_self = !this.data.currtab, obj = is_self ? this.data.request.issue : this.data.request.receive
    console.log(e)
    
    this.setData(is_self ? {
      'request.issue.page.page': 1,
      'request.issue.hasNextPage': true,
      'request.issue.onShow': true,
    } : {
      'request.receive.page.page': 1,
      'request.receive.hasNextPage': true,
      'request.receive.onShow': true,
    }, () => {
      this.fetchRequest({
        complete: () => {
          this.setData({
            ['request.' + (is_self ? 'issue' : 'receive') + '.refresher']: false
          })
        }
      })
    })
    
    return false
  },
  fetchRequest(callback) {
    const is_self = !this.data.currtab, obj = is_self ? this.data.request.issue : this.data.request.receive
    if(this.data.request.fetching || !obj.hasNextPage) {
      return false
    }

    const field = is_self ? 'request.issue' : 'request.receive'
    this.setData({fetching: true})

    wx.showLoading({title: '请稍候', mask: true})
    ins.api.requestApi('request/list', 'POST', {
      self: is_self,
      page: obj.page
    }, {
      complete: (resp) => {
        this.setData({fetching: false})
        wx.hideLoading()
        
        if(callback != null && typeof callback.complete == "function") {
          callback.complete(resp)
        }
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          const data = resp.data.data
          if(obj.onShow) {
            this.setData({
              [field + '.onShow']: false,
              [field + '.list']: []
            })
          }

          this.setData({
            [field + '.list[' + obj.list.length + ']']: data
          })

          if(data.length < obj.page.offset) {
            this.setData({
              [field + '.hasNextPage']: false
            })
          } else {
            this.setData({
              [field + '.page.page']: obj.page.page
            })
          }
        } else {
          wx.showToast({
            title: resp.data.data != null ? resp.data.data[0] : '服务器错误',
            icon: 'none'
          })
        }

        if(callback != null && typeof callback.success == "function") {
          callback.success(resp)
        }
      },
      fail: (resp) => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })

        if(callback != null && typeof callback.fail == "function") {
          callback.fail(resp)
        }
      }
    })
  },
  orderShow: function () {
    switch (this.data.currtab) {
      case 0:
        if(this.data.request.issue.onShow || this.data.request.issue.list.length == 0 && this.data.request.issue.hasNextPage) {
          this.fetchRequest()
        }
        break
      case 1:
        if(this.data.request.receive.onShow || this.data.request.receive.list.length == 0 && this.data.request.receive.hasNextPage) {
          this.fetchRequest()
        }
        break
    }
  },
  toDetail(e) {
    const data = e.currentTarget.dataset, obj = this.data.request[data.field].list[data.block][data.index]
    
    wx.setStorageSync('request_detail', obj)
    wx.navigateTo({
      url: '/pages/orders/detail?id=' + obj.id + '&is_self=' + !this.data.currtab
    })
  }
})
